﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Manager.Domain.User;
using System.Diagnostics;
using BrnMall.Core;
using PetaPoco;

namespace BrnMall.Manager.Manager.User
{
    public static class CashOrderManager
    {
        /// <summary>
        /// 添加新订单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool Add(CashOrder model)
        {
            object obj = null;
            using (var db = new QDMallDatabase())
            {
                obj = db.Insert(model);
            }
            if (obj != null)
                return true;
            return false;
        }
        
        public static Page<CashOrder> GetListPage(int storeId, string nickName, string osn, int orderState, string startTime, string endTime, int page, int pageSize = 15)
        {
            var sql = new StringBuilder();
            sql.AppendFormat("select id,uid,storeid,CashItemId,osn,OrderAmount,GiveAmount,OrderState,IP,CreateTime,paysn from cash_cashorder where storeId={0}", storeId);


            if (!string.IsNullOrEmpty(nickName))
                sql.AppendFormat(" and uid in(0{0})", GetUid(nickName));

            if (!string.IsNullOrEmpty(osn))
                sql.AppendFormat(" and osn='{0}'", osn);

            if (!string.IsNullOrEmpty(osn))
                sql.AppendFormat(" and osn='{0}'", osn);

            if (orderState > 0)
                sql.AppendFormat(" and orderState={0}", orderState);

            if (!string.IsNullOrEmpty(startTime))
                sql.AppendFormat(" and CreateTime>='{0}'", startTime);

            if (!string.IsNullOrEmpty(endTime))
                sql.AppendFormat(" and CreateTime<='{0}'", endTime);

            sql.Append(" order by id desc");

            using (var db = new QDMallDatabase())
            {
                return db.Page<CashOrder>(page, pageSize, sql.ToString());
            }
        }

        static string GetUid(string nickName)
        {
            StringBuilder sbId = new StringBuilder();
            var list = UsersManager.GetIdsByNickName(nickName, 20);
            foreach (var item in list)
            {
                sbId.AppendFormat(",{0}", item);
            }
            return sbId.ToString();
        }

        public static CashOrder Get(int oid)
        {
            try
            {
                using (var db = new QDMallDatabase())
                {
                    return db.SingleOrDefault<CashOrder>(string.Format("select top 1 * from cash_cashorder where id={0}", oid));
                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                throw;
            }
        }
        
        public static void UpdatePrepayId(int oid, string prepayId)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update cash_cashorder set prepay_id=@0 where id=@1", prepayId, oid);
            }
        }
        /// <summary>
        /// 更改订单状态
        /// </summary>
        /// <param name="oid">订单id</param>
        /// <param name="orderState">订单状态</param>
        /// <param name="paySN">支付单号</param>
        /// <param name="payTime">支付时间</param>
        public static int PayOrder(int oid, OrderState orderState, string paySN, DateTime payTime)
        {
            int state = (int)orderState;
            string sql = "update cash_cashorder set orderState=@0,paySN=@1,payTime=@2 where id=@3";
            using (var db = new QDMallDatabase())
            {
                return db.Execute(sql, state, paySN, payTime, oid);
            }
        }
    }
}
